import Vue from 'vue'
import Router from 'vue-router'
import Storage from "@/utils/storage";

import home from './router/home'

import my from './router/my'
import supply from './router/supply' // 我要供货 模块

import helppoor from './router/helppoor'    // 扶贫项目 模块

import buy from './router/buy'    // 扶贫项目 模块


Vue.use(Router)

const routes = [{
		path: '/',
		name: 'home',
		component: () => import('./views/home/Home.vue')
	},
	//首页
	{
		path: '/home',
		name: 'home',
		component: () => import('./views/home/Home.vue'),
		children: [
			...home
		]
	},
	//  我要采购
	{
		path: '/purchase',
		name: 'purchase',
		component: () => import('./views/purchase/Purchase.vue'),
		meta: {
            navTitle: '采购中心'
        }
	},
	// 我要求购
	{
		path: '/buy',
		name: 'buy',
		component: () => import('./views/buy/Buy.vue'),
		children: [
			...buy
		],
		meta: {
            navTitle: '求购中心',
			requireAuth: true
        }
	},
	// 我要供货
	{
		path: '/supply',
		name: 'supply',
		component: () => import('./views/supply/Supply.vue'),
		children: [
			...supply
		],
		meta: {
            navTitle: '供货中心'
        }
	},
	// 我要供货
	{
		path: '/helppoor',
		name: 'helppoor',
		component: () => import('./views/helppoor/HelpPoor.vue'),
		children: [
			...helppoor
		],
		meta: {
	        navTitle: '扶贫项目'
	    }
	},
	
	{
		path: '/my',
		name: 'my',
		component: () => import('./views/my/My.vue'),
		children: [
			...my
		]
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('./views/login/Login.vue'),
		meta: {
			navTitle: '友牧农场'
		}
	},
	{
		path: '/wxLogin',
		name: 'wxLogin',
		component: () => import('@/views/login/WxLogin.vue'),
		meta: {
			navTitle: '微信登录'
		}
	},
	{
		path: '/bindWx',
		name: 'bindWx',
		component: () => import('@/views/login/BindWx.vue'),
		meta: {
			navTitle: '绑定微信'
		}
	},
	{
		path: '/loginByAccount',
		name: 'loginByAccount',
		component: () => import('@/views/login/LoginByAccount.vue'),
		meta: {
			navTitle: '登录'
		}
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('./views/login/Register.vue'),
		meta: {
			navTitle: '注册'
		}
	},
	{
		path: '/forgetPassword',
		name: 'forgetPassword',
		component: () => import('./views/login/ForgetPassword.vue'),
		meta: {
			navTitle: '忘记密码'
		}
	},
	{
		path: '/404',
		name: '404',
		component: () => import('./views/404.vue')
	},
	{
		path: '/refresh',
		name: 'refresh',
		component: () => import('./views/Refresh.vue')
	},
	{
		path: '*',
		redirect: '/'
	}
]

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 分模块的路由，合并传入Router
let router = new Router({
	// mode: 'history',
	routes: routes
})

// 登录拦截
router.beforeEach((to, from, next) => {
	if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
		if (Storage.get("token")) { // 判断当前的token是否存在
			next();
		} else {
			next({
				path: '/login',
				query: {
					redirect: '/'
				}
			})
		}
	} else {
		next();
	}
});

export default router
